package com.asa.mvvm_aop_frame.basic.aspect

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.util.Log
import com.asa.mvvm_aop_frame.basic.pointcut.ActivityLifecyclePoint.ON_ACTIVITY_CREATE
import com.asa.mvvm_aop_frame.basic.pointcut.ActivityLifecyclePoint.ON_ACTIVITY_DESTROY
import com.asa.mvvm_aop_frame.basic.pointcut.ActivityLifecyclePoint.ON_ACTIVITY_NEW_INTENT
import com.asa.mvvm_aop_frame.basic.pointcut.ActivityLifecyclePoint.ON_ACTIVITY_PAUSE
import com.asa.mvvm_aop_frame.basic.pointcut.ActivityLifecyclePoint.ON_ACTIVITY_RESULT
import com.asa.mvvm_aop_frame.basic.pointcut.ActivityLifecyclePoint.ON_ACTIVITY_RESUME
import com.asa.mvvm_aop_frame.basic.pointcut.ActivityLifecyclePoint.ON_ACTIVITY_SAVE_INSTANCE_STATE
import com.asa.mvvm_aop_frame.basic.pointcut.ActivityLifecyclePoint.ON_ACTIVITY_START
import com.asa.mvvm_aop_frame.basic.pointcut.ActivityLifecyclePoint.ON_ACTIVITY_STOP
import com.asa.mvvm_aop_frame.basic.ui.BaseActivity
import org.aspectj.lang.JoinPoint
import org.aspectj.lang.annotation.After
import org.aspectj.lang.annotation.Aspect
import java.lang.Exception

/**
 * create By：anderson
 * on 2022/4/27
 * desc:
 */
@Aspect
class ActivityLifecycleAspect {


    @After(ON_ACTIVITY_CREATE)
    fun onActivityCreatedAspect(joinPoint: JoinPoint) {
        try {
            if (joinPoint.`this` !is BaseActivity<*, *>) {
                throw IllegalStateException("joinPoint is not BaseActivity")
            }

            val baseActivity = joinPoint.`this` as BaseActivity<*, *>
            val application = baseActivity.application
            val declaredMethod =
                application.javaClass.getDeclaredMethod(
                    "onActivityCreate",
                    Activity::class.java,
                    Bundle::class.java
                )
            declaredMethod.invoke(application, baseActivity, joinPoint.args[0])
        } catch (e: Exception) {

        }
    }

    @After(ON_ACTIVITY_NEW_INTENT)
    open fun onActivityNewIntentAspect(joinPoint: JoinPoint) {
        try {
            if (joinPoint.`this` !is BaseActivity<*, *>) {
                throw IllegalStateException("joinPoint is not BaseActivity")
            }
            val baseActivity = joinPoint.`this` as BaseActivity<*, *>
            val application = baseActivity.application
            val declaredMethod =
                application.javaClass.getDeclaredMethod(
                    "onActivityNewIntent",
                    Activity::class.java,
                    Intent::class.java
                )
            declaredMethod.invoke(application, baseActivity, joinPoint.args[0])
        } catch (e: Exception) {

        }
    }

    @After(ON_ACTIVITY_SAVE_INSTANCE_STATE)
    open fun onActivitySaveInstanceStateAspect(joinPoint: JoinPoint) {
        try {
            if (joinPoint.`this` !is BaseActivity<*, *>) {
                throw IllegalStateException("joinPoint is not BaseActivity")
            }
            val baseActivity = joinPoint.`this` as BaseActivity<*, *>
            val application = baseActivity.application
            val declaredMethod =
                application.javaClass.getDeclaredMethod(
                    "onActivitySaveInstanceState",
                    Activity::class.java,
                    Bundle::class.java
                )
            declaredMethod.invoke(application, baseActivity, joinPoint.args[0])
        } catch (e: Exception) {

        }
    }

    @After(ON_ACTIVITY_RESULT)
    open fun onActivityResultAspect(joinPoint: JoinPoint) {
        try {
            if (joinPoint.`this` !is BaseActivity<*, *>) {
                throw IllegalStateException("joinPoint is not BaseActivity")
            }
            val baseActivity = joinPoint.`this` as BaseActivity<*, *>
            val application = baseActivity.application
            val declaredMethod =
                application.javaClass.getDeclaredMethod(
                    "onActivityResult",
                    Activity::class.java,
                    Int::class.java,
                    Int::class.java,
                    Intent::class.java
                )
            declaredMethod.invoke(
                application, baseActivity,
                joinPoint.args[0],
                joinPoint.args[1],
                joinPoint.args[2]
            )
        } catch (e: Exception) {

        }
    }

    @After(ON_ACTIVITY_START)
    open fun onActivityStartAspect(joinPoint: JoinPoint) {
        try {
            if (joinPoint.`this` !is BaseActivity<*, *>) {
                throw IllegalStateException("joinPoint is not BaseActivity")
            }
            val baseActivity = joinPoint.`this` as BaseActivity<*, *>
            val application = baseActivity.application
            val declaredMethod =
                application.javaClass.getDeclaredMethod("onActivityStart", Activity::class.java)
            declaredMethod.invoke(application, baseActivity)
        } catch (e: Exception) {

        }
    }

    @After(ON_ACTIVITY_RESUME)
    open fun onActivityResumeAspect(joinPoint: JoinPoint) {
        try {
            if (joinPoint.`this` !is BaseActivity<*, *>) {
                throw IllegalStateException("joinPoint is not BaseActivity")
            }
            val baseActivity = joinPoint.`this` as BaseActivity<*, *>
            val application = baseActivity.application
            val declaredMethod =
                application.javaClass.getDeclaredMethod("onActivityResume", Activity::class.java)
            declaredMethod.invoke(application, baseActivity)
        } catch (e: Exception) {

        }
    }

    @After(ON_ACTIVITY_PAUSE)
    open fun onActivityPauseAspect(joinPoint: JoinPoint) {
        try {
            if (joinPoint.`this` !is BaseActivity<*, *>) {
                throw IllegalStateException("joinPoint is not BaseActivity")
            }
            val baseActivity = joinPoint.`this` as BaseActivity<*, *>
            val application = baseActivity.application
            val declaredMethod =
                application.javaClass.getDeclaredMethod("onActivityPause", Activity::class.java)
            declaredMethod.invoke(application, baseActivity)
        } catch (e: Exception) {

        }
    }

    @After(ON_ACTIVITY_STOP)
    open fun onActivityStopAspect(joinPoint: JoinPoint) {
        try {
            if (joinPoint.`this` !is BaseActivity<*, *>) {
                throw IllegalStateException("joinPoint is not BaseActivity")
            }
            val baseActivity = joinPoint.`this` as BaseActivity<*, *>
            val application = baseActivity.application
            val declaredMethod =
                application.javaClass.getDeclaredMethod("onActivityStop", Activity::class.java)
            declaredMethod.invoke(application, baseActivity)
        } catch (e: Exception) {

        }
    }

    @After(ON_ACTIVITY_DESTROY)
    open fun onActivityDestroyAspect(joinPoint: JoinPoint) {
        try {
            if (joinPoint.`this` !is BaseActivity<*, *>) {
                throw IllegalStateException("joinPoint is not BaseActivity")
            }
            val baseActivity = joinPoint.`this` as BaseActivity<*, *>
            val application = baseActivity.application
            val declaredMethod =
                application.javaClass.getDeclaredMethod("onActivityDestroy", Activity::class.java)
            declaredMethod.invoke(application, baseActivity)
        } catch (e: Exception) {

        }
    }
}